// JavaScript Document
// JavaScript Document
// JavaScript Document
function createRequestObject(){
    var req;
    if(window.XMLHttpRequest){
        //For Firefox, Safari, Opera
        req = new XMLHttpRequest();
    }
    else if(window.ActiveXObject){
        //For IE 5+
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else{
        //Error for an old browser
        alert('Your browser is not IE 5 or higher, or Firefox or Safari or Opera');
    }
    return req;
}

//Make the XMLHttpRequest Object
var http = createRequestObject();
function sendRequestLoaiSP(method, url)
{
    if(method == 'get' || method == 'GET'){
        http.open(method,url);
        http.onreadystatechange = handleResponseLoaiSP;
        http.send(null);
    }
}

function sendRequestLoaiDM2(method, url)
{
    if(method == 'get' || method == 'GET'){
        http.open(method,url);
        http.onreadystatechange = handleResponseLoaiDM2;
        http.send(null);
    }
}

function sendRequestDanhMuc(method, url)
{
    if(method == 'get' || method == 'GET'){
        http.open(method,url);
        http.onreadystatechange = handleResponseDanhMuc;
        http.send(null);
    }
}

function sendRequestDongSanPham(method, url)
{
    if(method == 'get' || method == 'GET'){
        http.open(method,url);
        http.onreadystatechange = handleResponseDongSanPham;
        http.send(null);
    }
}

function handleResponseLoaiSP(){
    if(http.readyState == 4 && http.status == 200){
        var response = http.responseText;
        if(response){
            document.getElementById("idloaidm").innerHTML = response;
        }
    }     
}

function handleResponseLoaiDM2(){
    if(http.readyState == 4 && http.status == 200){
        var response = http.responseText;
        if(response){
            document.getElementById("loaiDanhMuc").innerHTML = response;
        }
    }     
}

function handleResponseDanhMuc(){
    if(http.readyState == 4 && http.status == 200){
        var response = http.responseText;
        if(response){
            document.getElementById("DanhMuc").innerHTML = response;
        }
    }     
}

function handleResponseDongSanPham(){
    if(http.readyState == 4 && http.status == 200){
        var response = http.responseText;
        if(response){
            document.getElementById("dongSanPham").innerHTML = response;
        }
    }     
}

function getLoaiSPDropdown()
{
   
    var loaisp = document.getElementById('loaiSP').options[document.getElementById('loaiSP').selectedIndex].value
    sendRequestLoaiSP('GET','LayLoaiDMTheoMaLoaiSP2?idloaisanpham=' + loaisp);
}

function getLoaiDM2Dropdown()
{
   
    var idloaidanhmuc = document.getElementById('loaiDM').options[document.getElementById('loaiDM').selectedIndex].value;
    sendRequestLoaiDM2('GET','LayDMTheoMaLoaiDM?idloaidanhmuc=' + idloaidanhmuc);
}


function getDanhMucDropdown()
{
   
    var iddanhmuc = document.getElementById('danhMuc').options[document.getElementById('danhMuc').selectedIndex].value;
    sendRequestDanhMuc('GET','LayDongSanPhamTheoDM?iddanhmuc=' + iddanhmuc);
}

function getDongSanPhamDropdown()
{
   
    var dongsp = document.getElementById('dongSanPham').options[document.getElementById('dongSanPham').selectedIndex].value;
    sendRequestDongSanPham('GET','ThemSanPhamController?dongsanpham=' + dongsp);
}


var intTextBox=0;

function addChiTietHangHoa()
{
intTextBox = intTextBox + 1;
var value = document.getElementById('listChiTietHangHoa').options[document.getElementById('listChiTietHangHoa').selectedIndex].value;
var text = document.getElementById('listChiTietHangHoa').options[document.getElementById('listChiTietHangHoa').selectedIndex].text;
var contentID = document.getElementById("TextBoxesGroup");
var newTBDiv = document.createElement("div");
newTBDiv.setAttribute("id","strText"+intTextBox);
newTBDiv.innerHTML = text+"&nbsp; : &nbsp;<input class='at-box-content' type='text' id='tfGiaTriChiTiet' name='tfGiaTriChiTiet'><input name='hfMaChiTiet' type='hidden' id='hfMaChiTiet' value="+value+" />";
contentID.appendChild(newTBDiv);
}

//FUNCTION TO REMOVE TEXT BOX ELEMENT
function removeChiTietHangHoa()
{
if(intTextBox != 0)
{
var contentID = document.getElementById("TextBoxesGroup");
contentID.removeChild(document.getElementById("strText"+intTextBox));
intTextBox = intTextBox-1;
}
}
